package com.retail.bbq.cola.exception;

import com.retail.bbq.cola.enums.ErrorCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BadRequestException.class)
    public ResponseEntity<ErrorResponse> handleBadRequest(BadRequestException ex) {
        ErrorResponse error = new ErrorResponse(ErrorCodeEnum.PARAM_ERROR.getCode(), ex.getMessage());
        return ResponseEntity.badRequest().body(error);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleException(Exception e) {
        log.error("统一异常处理，系统异常:", e);
        ErrorResponse error = new ErrorResponse(ErrorCodeEnum.SERVER_ERROR.getCode(), "系统繁忙，请稍后再试");
        return ResponseEntity.badRequest().body(error);
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class ErrorResponse {
        private int code;
        private String msg;
    }
}
